home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gdevpcfb.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  22KB  |  858 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevpcfb.c */
  20. /* IBM PC EGA and VGA display drivers for Ghostscript */
  21. /* This is fundamentally an EGA driver with some parameters */
  22. /* that allow it to drive larger displays. */
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gxdevice.h"
  27. #include "gdevpcfb.h"
  28.  
  29. /* Macro for casting gx_device argument */
  30. #define fb_dev ((gx_device_ega *)dev)
  31.  
  32. /* Define the color spectrum */
  33. #define black 0
  34. #define blue 1
  35. #define green 2
  36. #define cyan 3
  37. #define red 4
  38. #define magenta 5
  39. #define brown 6
  40. #define white 7
  41. #define dgray 8                /* dark gray is not very usable */
  42. #define lblue 9
  43. #define lgreen 10
  44. #define lcyan 11
  45. #define lred 12
  46. #define lmagenta 13
  47. #define yellow 14
  48. #define bwhite 15
  49.  
  50. /* Procedure record */
  51. private gx_device_procs ega_procs = {
  52.     ega_open,
  53.     gx_default_get_initial_matrix,
  54.     gx_default_sync_output,
  55.     gx_default_output_page,
  56.     ega_close,
  57.     ega_map_rgb_color,
  58.     ega_map_color_rgb,
  59.     ega_fill_rectangle,
  60.     ega_tile_rectangle,
  61.     ega_copy_mono,
  62.     ega_copy_color,
  63.     gx_default_draw_line,
  64.     ega_get_bits,
  65.     gx_default_get_props,
  66.     gx_default_put_props
  67. };
  68.  
  69. /* All the known instances */
  70.         /* EGA */
  71. gx_device_ega gs_ega_device =
  72.     ega_device("ega", ega_procs, 80, 350, 48.0/35.0, 0x10);
  73.         /* VGA */
  74. gx_device_ega gs_vga_device =
  75.     ega_device("vga", ega_procs, 80, 480, 1.0, 0x12);
  76.         /* ATI Wonder SuperVGA, 800x600, 16-color mode */
  77. gx_device_ega gs_atiw16_device =
  78.     ega_device("atiw16", ega_procs, 100, 600, 1.0, 0x54);
  79.         /* Trident SuperVGA, 800x600, 16-color mode */
  80. gx_device_ega gs_tvga16_device =
  81.     ega_device("tvga16", ega_procs, 100, 600, 1.0, 0x5b);
  82.         /* Tseng Labs SuperVGA, 800x600, 16-color mode */
  83. gx_device_ega gs_tseng16_device =
  84.     ega_device("tseng16", ega_procs, 100, 600, 1.0, 0x29);
  85.  
  86. /* Save the EGA mode */
  87. private int ega_save_mode = -1;
  88.  
  89. /* Initialize the EGA for graphics mode */
  90. int
  91. ega_open(gx_device *dev)
  92. {    /* Adjust the device resolution. */
  93.     /* This is a hack, pending refactoring of the put_props machinery. */
  94.     switch ( fb_dev->video_mode )
  95.     {
  96.     case 0x10:    /* EGA */
  97.         gx_device_adjust_resolution(dev, 640, 350, 1); break;
  98.     case 0x12:    /* VGA */
  99.         gx_device_adjust_resolution(dev, 640, 480, 1); break;
  100.     case 0x54:      /* ATI Wonder */
  101.     case 0x5b:    /* Trident */
  102.     case 0x29:    /* Tseng */
  103.         gx_device_adjust_resolution(dev, 800, 600, 1); break;
  104.     }
  105.     if ( ega_save_mode < 0 )
  106.         ega_save_mode = ega_get_mode();
  107.     /* Do implementation-specific initialization */
  108.     ega_set_signals(dev);
  109.     ega_set_mode(fb_dev->video_mode);
  110.     set_s_map(-1);            /* enable all maps */
  111.     return 0;
  112. }
  113.  
  114. /* Reinitialize the EGA for text mode */
  115. int
  116. ega_close(gx_device *dev)
  117. {    if ( ega_save_mode >= 0 )
  118.         ega_set_mode(ega_save_mode);
  119.     return 0;
  120. }
  121.  
  122. /* Map a r-g-b color to an EGA color code. */
  123. gx_color_index
  124. ega_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  125.   gx_color_value b)
  126. {
  127. #define c12 (gx_max_color_value / 2)
  128. #define c13 (gx_max_color_value / 3)
  129. #define c23 (gx_max_color_value - c13)
  130. #define cmono() ((gx_color_index)mono_color[r >> (gx_color_value_bits - 2)])
  131. #if ega_bits_of_color == 0        /* monochrome */
  132.     static const byte mono_color[4] = { black, white, white, bwhite };
  133.     return cmono();
  134. #else
  135. # if ega_bits_of_color == 1
  136.     static const byte mono_color[4] = { black, white, white, bwhite };
  137.     if ( r == g && g == b )
  138.         return cmono();
  139.     return (gx_color_index)
  140.         ((r > c12 ? 4 : 0) |
  141.          (g > c12 ? 10 : 0) |
  142.          (b > c12 ? 1 : 0));
  143. # else
  144.     static const byte g0[3][3] =
  145.      {{black,blue,lblue},{red,magenta,lmagenta},{lred,lmagenta,lmagenta}};
  146.     static const byte g1[3][3] =
  147.      {{green,cyan,lcyan},{brown,white,lcyan},{yellow,yellow,lmagenta}};
  148.     static const byte g2[3][3] =
  149.      {{lgreen,lgreen,lcyan},{lgreen,lgreen,lcyan},{yellow,yellow,bwhite}};
  150.     return (gx_color_index)
  151.         ((g >= c23 ? g2 : g >= c13 ? g1 : g0)
  152.          [r >= c23 ? 2 : r >= c13 ? 1 : 0]
  153.          [b >= c23 ? 2 : b >= c13 ? 1 : 0]);
  154. # endif
  155. #endif
  156. #undef c12
  157. #undef c13
  158. #undef c23
  159. }
  160.  
  161. /* Map a color code to r-g-b. */
  162. int
  163. ega_map_color_rgb(gx_device *dev, gx_color_index color,
  164.   gx_color_value prgb[3])
  165. {
  166. #define icolor (int)color
  167. #if rgb_max > 1
  168.     gx_color_value one =
  169.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  170. #else
  171. #  define one (gx_max_color_value / 2 + 1)
  172. #endif
  173.     prgb[0] = (icolor & 4 ? one : 0);
  174.     prgb[1] = (icolor & 2 ? one : 0);
  175.     prgb[2] = (icolor & 1 ? one : 0);
  176.     return 0;
  177. #undef one
  178. #undef icolor
  179. }
  180.  
  181. /* ------ Internal routines ------ */
  182.  
  183. /* Structure for operation parameters. */
  184. /* Note that this structure is known to assembly code. */
  185. /* Not all parameters are used for every operation. */
  186. typedef struct rop_params_s {
  187.     fb_ptr dest;            /* pointer to frame buffer */
  188.     int draster;            /* raster of frame buffer */
  189.     const byte *src;        /* pointer to source data */
  190.     int sraster;            /* source raster */
  191.     int width;            /* width in bytes */
  192.     int height;            /* height in scan lines */
  193.     int shift;            /* amount to right shift source */
  194.     int invert;            /* 0 or -1 to invert source */
  195.     int data;            /* data for fill */
  196. } rop_params;
  197. typedef rop_params _ss *rop_ptr;
  198.  
  199. /* Assembly language routines */
  200.  
  201. #if USE_ASM
  202. void memsetcol(P1(rop_ptr)); /* dest, draster, height, data */
  203. #else
  204. #define memsetcol cmemsetcol
  205. private void
  206. cmemsetcol(rop_ptr rop)
  207. {    byte *addr = rop->dest;
  208.     int yc = rop->height;
  209.     byte data = rop->data;
  210.     int draster = rop->draster;
  211.     while ( yc-- )
  212.      { byte_discard(*addr);
  213.        *addr = data;
  214.        addr += draster;
  215.      }
  216. }
  217. #endif
  218.  
  219. #if USE_ASM
  220. void memsetrect(P1(rop_ptr)); /* dest, draster, width, height, data */
  221. #else
  222. #define memsetrect cmemsetrect
  223. private void
  224. cmemsetrect(rop_ptr rop)
  225. {    int yc = rop->height;
  226.     int width = rop->width;
  227.     if ( yc <= 0 || width <= 0 ) return;
  228.        {    byte *addr = rop->dest;
  229.         byte data = rop->data;
  230.         if ( width > 5 )    /* use memset */
  231.            {    int skip = rop->draster;
  232.             do
  233.                {    memset(addr, data, width);
  234.                 addr += skip;
  235.                }
  236.             while ( --yc );
  237.            }
  238.         else            /* avoid the fixed overhead */
  239.            {    int skip = rop->draster - width;
  240.             do
  241.                {    int cnt = width;
  242.                 do { *addr++ = data; } while ( --cnt );
  243.                 addr += skip;
  244.                }
  245.             while ( --yc );
  246.            }
  247.        }
  248. }
  249. #endif
  250.  
  251. #if USE_ASM
  252. void memrwcol(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  253. #  define memrwcol0(rop) memrwcol(rop)    /* same except shift = 0 */
  254. #else
  255. #  define memrwcol cmemrwcol
  256. #  define memrwcol0 cmemrwcol0
  257. private void
  258. cmemrwcol(rop_ptr rop)
  259. {    byte *dp = rop->dest;
  260.     const byte *sp = rop->src;
  261.     int yc = rop->height;
  262.     int shift = rop->shift;
  263.     byte invert = rop->invert;
  264.     int sraster = rop->sraster, draster = rop->draster;
  265.     while ( yc-- )
  266.      { byte_discard(*dp);
  267.        *dp = ((*sp >> shift) + (*sp << (8 - shift))) ^ invert;
  268.        dp += draster, sp += sraster;
  269.      }
  270. }
  271. private void
  272. cmemrwcol0(rop_ptr rop)
  273. {    byte *dp = rop->dest;
  274.     const byte *sp = rop->src;
  275.     int yc = rop->height;
  276.     byte invert = rop->invert;
  277.     int sraster = rop->sraster, draster = rop->draster;
  278.     if ( yc > 0 ) do
  279.      { byte_discard(*dp);
  280.        *dp = *sp ^ invert;
  281.        dp += draster, sp += sraster;
  282.      }
  283.     while ( --yc );
  284. }
  285. #endif
  286.  
  287. #if USE_ASM
  288. void memrwcol2(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  289. #else
  290. #define memrwcol2 cmemrwcol2
  291. private void
  292. cmemrwcol2(rop_ptr rop)
  293. {    byte *dp = rop->dest;
  294.     const byte *sp = rop->src;
  295.     int yc = rop->height;
  296.     int shift = rop->shift;
  297.     byte invert = rop->invert;
  298.     int sraster = rop->sraster, draster = rop->draster;
  299.     while ( yc-- )
  300.      { byte_discard(*dp);
  301.        *dp = ((sp[1] >> shift) + (*sp << (8 - shift))) ^ invert;
  302.        dp += draster, sp += sraster;
  303.      }
  304. }
  305. #endif
  306.  
  307. /* Forward definitions */
  308. int ega_write_dot(P4(gx_device *, int, int, gx_color_index));
  309. private void near fill_rectangle(P4(rop_ptr, int, int, int));
  310. private void near fill_row_only(P4(byte *, int, int, int));
  311.  
  312. /* Clean up after writing */
  313. #define dot_end()\
  314.   set_g_mask(0xff)            /* all bits on */
  315.  
  316. /* Write a dot using the EGA color codes. */
  317. /* This doesn't have to be efficient. */
  318. int
  319. ega_write_dot(gx_device *dev, int x, int y, gx_color_index color)
  320. {    byte data[4];
  321.     data[0] = (byte)color;
  322.     return ega_copy_color(dev, data, 1, 4, gx_no_bitmap_id, x, y, 1, 1);
  323. }
  324.  
  325. /* Macro for testing bit-inclusion */
  326. #define bit_included_in(x,y) !((x)&~(y))
  327.  
  328. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  329. /* Color = gx_no_color_index means transparent (no effect on the image). */
  330. int
  331. ega_copy_mono(gx_device *dev,
  332.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  333.   int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  334. {    rop_params params;
  335. #define czero (int)izero
  336. #define cone (int)ione
  337.     int dleft, count;
  338.     byte mask, rmask;
  339.     fb_ptr save_dest;
  340.     int other_color = -1;
  341.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  342.     params.dest = mk_fb_ptr(x, y);
  343.     params.draster = fb_dev->raster;
  344.     params.src = base + (sourcex >> 3);
  345.     params.sraster = raster;
  346.     params.height = h;
  347.     params.shift = (x - sourcex) & 7;
  348.     /* Analyze the 16 possible cases: each of izero and ione may be */
  349.     /* 0, 0xf, transparent, or some other color. */
  350.     switch ( czero )
  351.        {
  352.     case no_color:
  353.         switch ( cone )
  354.            {
  355.         default:        /* (T, other) */
  356.             /* Must do 2 passes */
  357.             other_color = cone;
  358.             save_dest = params.dest;
  359.             /* falls through */
  360.         case 0:            /* (T, 0) */
  361.             set_g_function(gf_AND);
  362.             params.invert = -1;
  363.             break;
  364.         case 0xf:        /* (T, 0xf) */
  365.             set_g_function(gf_OR);
  366.             params.invert = 0;
  367.             break;
  368.         case no_color:        /* (T, T) */
  369.             return 0;    /* nothing to do */
  370.            }
  371.         break;
  372.     case 0:
  373.         params.invert = 0;
  374.         switch ( cone )
  375.            {
  376.         default:        /* (0, other) */
  377.             set_g_const(0);
  378.             set_g_const_map(cone ^ 0xf);
  379.             /* falls through */
  380.         case 0xf:        /* (0, 0xf) */
  381.             break;
  382.         case no_color:        /* (0, T) */
  383.             set_g_function(gf_AND);
  384.             break;
  385.            }
  386.         break;
  387.     case 0xf:
  388.         params.invert = -1;
  389.         switch ( cone )
  390.            {
  391.         case 0:            /* (0xf, 0) */
  392.             break;
  393.         default:        /* (0xf, other) */
  394.             set_g_const(0xf);
  395.             set_g_const_map(cone);
  396.             break;
  397.         case no_color:        /* (0xf, T) */
  398.             set_g_function(gf_OR);
  399.             /* falls through */
  400.            }
  401.         break;
  402.     default:
  403.         switch ( cone )
  404.            {
  405.         default:        /* (other, not T) */
  406.             if ( bit_included_in(czero, cone) )
  407.                {    set_g_const(czero);
  408.                 set_g_const_map(czero ^ cone ^ 0xf);
  409.                 params.invert = 0;
  410.                 break;
  411.                }
  412.             else if ( bit_included_in(cone, czero) )
  413.                {    set_g_const(cone);
  414.                 set_g_const_map(cone ^ czero ^ 0xf);
  415.                 params.invert = -1;
  416.                 break;
  417.                }
  418.             /* No way around it, fill with one color first. */
  419.             save_dest = params.dest;
  420.             fill_rectangle((rop_ptr)¶ms, x & 7, w, cone);
  421.             params.dest = save_dest;
  422.             set_g_function(gf_XOR);
  423.             set_s_map(czero ^ cone);
  424.             other_color = -2;    /* must reset s_map at end */
  425.             params.invert = -1;
  426.             break;
  427.         case no_color:        /* (other, T) */
  428.             /* Must do 2 passes */
  429.             other_color = czero;
  430.             save_dest = params.dest;
  431.             set_g_function(gf_AND);
  432.             params.invert = 0;
  433.             break;
  434.            }
  435.         break;
  436.        }
  437.     /* Actually copy the bits. */
  438.     dleft = 8 - (x & 7);
  439.     mask = 0xff >> (8 - dleft);
  440.     count = w - dleft;
  441.     if ( count < 0 )
  442.         mask -= mask >> w,
  443.         rmask = 0;
  444.     else
  445.         rmask = 0xff00 >> (count & 7);
  446.     /* params: dest, src, sraster, height, shift, invert */
  447.     /* Smashes params.src, params.dest, count. */
  448. copy:    set_g_mask(mask);
  449.     if ( params.shift == 0 )    /* optimize the aligned case */
  450.        {    /* Do left column */
  451.         memrwcol0((rop_ptr)¶ms);
  452.         /* Do center */
  453.         if ( (count -= 8) >= 0 )
  454.            {    out_g_mask(0xff);
  455.             do
  456.                {    params.src++, params.dest++;
  457.                 memrwcol0((rop_ptr)¶ms);
  458.                }
  459.             while ( (count -= 8) >= 0 );
  460.            }
  461.         /* Do right column */
  462.         if ( rmask )
  463.            {    params.src++, params.dest++;
  464.             out_g_mask(rmask);
  465.             memrwcol0((rop_ptr)¶ms);
  466.            }
  467.        }
  468.     else
  469.        {    /* Do left column */
  470.         int sleft = 8 - (sourcex & 7);
  471.         if ( sleft >= dleft )
  472.            {    /* Source fits in one byte */
  473.             memrwcol((rop_ptr)¶ms);
  474.            }
  475.         else if ( w <= sleft )
  476.            {    /* Source fits in one byte, thin case */
  477.             memrwcol((rop_ptr)¶ms);
  478.             goto fin;
  479.            }
  480.         else
  481.            {    memrwcol2((rop_ptr)¶ms);
  482.             params.src++;
  483.            }
  484.         /* Do center */
  485.         if ( (count -= 8) >= 0 )
  486.            {    out_g_mask(0xff);
  487.             do
  488.                {    params.dest++;
  489.                 memrwcol2((rop_ptr)¶ms);
  490.                 params.src++;
  491.                }
  492.             while ( (count -= 8) >= 0 );
  493.            }
  494.         /* Do right column */
  495.         if ( rmask )
  496.            {    out_g_mask(rmask);
  497.             params.dest++;
  498.             if ( count + 8 <= params.shift )
  499.                 memrwcol((rop_ptr)¶ms);
  500.             else
  501.                 memrwcol2((rop_ptr)¶ms);
  502.            }
  503.        }
  504. fin:    if ( other_color != -1 )
  505.        {    if ( other_color >= 0 )
  506.            {    /* Do the second pass on (T, other) or (other, T). */
  507.             count = w - dleft;
  508.             params.src = base + (sourcex >> 3);
  509.             params.dest = save_dest;
  510.             params.invert ^= -1;
  511.             set_s_map(other_color);
  512.             set_g_function(gf_OR);
  513.             other_color = -2;
  514.             goto copy;
  515.            }
  516.         else
  517.            {    /* Finished second pass, restore s_map */
  518.             set_s_map(-1);
  519.            }
  520.        }
  521.     set_g_function(gf_WRITE);
  522.     set_g_const_map(0);
  523.     dot_end();
  524.     return 0;
  525. #undef czero
  526. #undef cone
  527. }
  528.  
  529. /* Copy a color pixelmap.  This is just like a bitmap, */
  530. /* except that each pixel takes 4 bits instead of 1. */
  531. int
  532. ega_copy_color(gx_device *dev,
  533.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  534.   int x, int y, int w, int h)
  535. {    const byte *line = base + (sourcex >> 1);
  536.     unsigned lmask = 0x80 >> (x & 7);
  537.     int bitx = sourcex & 1;
  538.     int ex = w + bitx;
  539.     fb_ptr fbline;
  540.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  541.     fbline = mk_fb_ptr(x, y);
  542.     set_g_mode(gm_FILL);
  543.     select_g_mask();
  544.     while ( --h >= 0 )
  545.     {    const byte *bptr = line;
  546.         int px = bitx;
  547.         /* Turbo C will put an unsigned in a register, */
  548.         /* but not a byte! */
  549.         register unsigned mask = lmask;
  550.         fb_ptr fbptr = fbline;
  551.         do
  552.            {    byte_discard(*fbptr);    /* latch frame buffer data */
  553.             out_g_mask(mask);
  554.             *fbptr = (px & 1 ? *bptr++ & 0xf : *bptr >> 4);
  555.             if ( (mask >>= 1) == 0 )
  556.                 mask = 0x80, fbptr++;
  557.            }
  558.         while ( ++px < ex );
  559.         line += raster;
  560.         fbline += fb_dev->raster;
  561.     }
  562.     set_g_mode(gm_DATA);
  563.     dot_end();
  564.     return 0;
  565. }
  566.  
  567. /* Fill a rectangle. */
  568. int
  569. ega_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  570.   gx_color_index color)
  571. {    rop_params params;
  572.     fit_fill(dev, x, y, w, h);
  573.     params.dest = mk_fb_ptr(x, y);
  574.     if ( h == 1 )
  575.         fill_row_only(params.dest, x & 7, w, (int)color);
  576.     else
  577.        {    params.draster = fb_dev->raster;
  578.         params.height = h;
  579.         fill_rectangle((rop_ptr)¶ms, x & 7, w, (int)color);
  580.         dot_end();
  581.        }
  582.     return 0;
  583. }
  584.  
  585. /* Tile a rectangle.  Note that the two colors must both be supplied, */
  586. /* i.e. neither one can be gx_no_color_index (transparent): */
  587. /* a transparent color means that the tile is colored, not a mask. */
  588. int
  589. ega_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  590.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  591.   int px, int py)
  592. #define zero (int)czero
  593. #define one (int)cone
  594. {    rop_params params;
  595.     int xmod, width_bytes;
  596.     int tile_height = tile->size.y;
  597.     int xbit;
  598.     int lcount;
  599.     int mask, rmask;
  600.     byte narrow;
  601.     byte again;
  602.     int const_bits, maps;
  603.     int ymod, yleft;
  604.     fit_fill(dev, x, y, w, h);
  605.     /* We only handle the easiest cases directly. */
  606.     if ( (tile->size.x & 7) || one == -1 || zero == -1 || px || py )
  607.         return gx_default_tile_rectangle(dev, tile, x, y, w, h,
  608.             czero, cone, px, py);
  609.     /* Following is similar to aligned case of copy_mono */    
  610.     params.dest = mk_fb_ptr(x, y);
  611.     params.draster = fb_dev->raster;
  612.     params.sraster = tile->raster;
  613.     params.shift = 0;
  614.     xbit = x & 7;
  615.     /* Set up the graphics registers */
  616.     const_bits = (zero ^ one) ^ 0xf;
  617.     if ( const_bits )
  618.        {    set_g_const(zero);    /* either color will do */
  619.         set_g_const_map(const_bits);
  620.        }
  621.     if ( (maps = zero & ~one) != 0 )
  622.        {    set_s_map(maps += const_bits);
  623.         params.invert = -1;
  624.         again = one & ~zero;
  625.        }
  626.     else
  627.        {    maps = one & ~zero;
  628.         set_s_map(maps += const_bits);
  629.         params.invert = 0;
  630.         again = 0;
  631.        }
  632.     xmod = (x % tile->size.x) >> 3;
  633.     width_bytes = tile->size.x >> 3;
  634.     mask = 0xff >> xbit;
  635.     if ( w + xbit <= 8 )
  636.         mask -= mask >> w,
  637.         rmask = 0,
  638.         narrow = 1;
  639.     else
  640.        {    rmask = (0xff00 >> ((w + x) & 7)) & 0xff;
  641.         if ( xbit )    w += xbit - 8;
  642.         else        mask = 0, --xmod, --params.dest;
  643.         narrow = 0;
  644.        }
  645.     ymod = y % tile_height;
  646. tile:    yleft = tile_height - ymod;
  647.     params.src = tile->data + ymod * params.sraster + xmod;
  648.     lcount = h;
  649.     if ( narrow )            /* Optimize narrow case */
  650.        {    set_g_mask(mask);
  651.         if ( lcount > yleft )
  652.            {    params.height = yleft;
  653.             memrwcol0((rop_ptr)¶ms);
  654.             params.dest += yleft * params.draster;
  655.             params.src = tile->data + xmod;
  656.             params.height = tile_height;
  657.             lcount -= yleft;
  658.             while ( lcount >= tile_height )
  659.                {    memrwcol0((rop_ptr)¶ms);
  660.                 params.dest += tile_height * params.draster;
  661.                 lcount -= tile_height;
  662.                }
  663.            }
  664.         if ( lcount )
  665.            {    params.height = lcount;
  666.             memrwcol0((rop_ptr)¶ms);
  667.            }
  668.        }
  669.     else
  670.        {    fb_ptr line = params.dest;
  671.         int xpos = width_bytes - xmod;
  672.         while ( 1 )
  673.            {    int xleft = xpos;
  674.             int count = w;
  675.             params.height = (lcount > yleft ? yleft : lcount);
  676.             /* Do first byte, if not a full byte. */
  677.             if ( mask )
  678.                {    set_g_mask(mask);
  679.                 memrwcol0((rop_ptr)¶ms);
  680.                }
  681.             /* Do full bytes */
  682.             if ( (count -= 8) >= 0 )
  683.                {    set_g_mask(0xff);
  684.                 do
  685.                    {    if ( !--xleft )
  686.                         xleft = width_bytes,
  687.                         params.src -= width_bytes;
  688.                     ++params.src, ++params.dest;
  689.                     memrwcol0((rop_ptr)¶ms);
  690.                    }
  691.                 while ( (count -= 8) >= 0 );
  692.                }
  693.             /* Do last byte */
  694.             if ( rmask )
  695.                {    if ( !--xleft )
  696.                     xleft = width_bytes,
  697.                     params.src -= width_bytes;
  698.                 set_g_mask(rmask);
  699.                 ++params.src, ++params.dest;
  700.                 memrwcol0((rop_ptr)¶ms);
  701.                }
  702.             if ( (lcount -= params.height) == 0 ) break;
  703.             params.dest = line += params.height * params.draster;
  704.             params.src = tile->data + xmod;
  705.             yleft = tile_height;
  706.            }
  707.        }
  708.     /* Now do the second color if needed */
  709.     if ( again )
  710.        {    maps = again + const_bits;
  711.         set_s_map(maps);
  712.         again = 0;
  713.         params.dest = mk_fb_ptr(x, y);
  714.         if ( mask == 0 ) params.dest--;
  715.         params.invert = 0;
  716.         goto tile;
  717.        }
  718.     if ( maps != 0xf )
  719.         set_s_map(-1);
  720.     if ( const_bits )
  721.         set_g_const_map(0);
  722.     dot_end();
  723.     return 0;
  724. }
  725.  
  726. /* Read scan lines back from the frame buffer. */
  727. int
  728. ega_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  729. {    uint bytes_per_row = dev->width >> 1;
  730.     int plane;
  731.     ushort *dest = (ushort *)data;
  732.     if ( y < 0 || y >= dev->height )
  733.         return gs_error_rangecheck;
  734.     memset(data, 0, bytes_per_row);
  735.     for ( plane = 0; plane < 4; plane++ )
  736.        {    fb_ptr src = mk_fb_ptr(0, y);
  737.         int i;
  738.         /* Plane 0 is the least significant plane. */
  739.         /* We know we're on a little-endian machine.... */
  740.         static const ushort spread4[16] =
  741.            {    0x0, 0x800, 0x8000, 0x8800,
  742.             0x8, 0x808, 0x8008, 0x8808,
  743.             0x80, 0x880, 0x8080, 0x8880,
  744.             0x88, 0x888, 0x8088, 0x8888
  745.            };
  746.         set_g_read_plane(plane);
  747.         for ( i = 0; i < dev->width; i += 8, src++, dest += 2 )
  748.            {    byte b = *src;
  749.             dest[0] = (dest[0] >> 1) + spread4[b >> 4];
  750.             dest[1] = (dest[1] >> 1) + spread4[b & 0xf];
  751.            }
  752.        }
  753.     if ( actual_data != 0 )
  754.         *actual_data = data;
  755.     return 0;
  756. }
  757.  
  758. /* ------ Internal routines ------ */
  759.  
  760. /* Mask table for rectangle fill. */
  761. static const byte rmask_tab[9] =
  762.    {    0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  763.    };
  764.  
  765. /* Fill a rectangle specified by pointer into frame buffer, */
  766. /* starting bit within byte, width, and height. */
  767. /* Smashes rop->dest. */
  768. private void near
  769. fill_rectangle(register rop_ptr rop, int bit, int w, int color)
  770.   /* rop: dest, draster, height */
  771. {    set_g_const(color);
  772.     set_g_const_map(0xf);
  773.     select_g_mask();
  774.     if ( bit + w <= 8 )
  775.        {    /* Less than one byte */
  776.         out_g_mask(rmask_tab[w] >> bit);
  777.         memsetcol(rop);
  778.        }
  779.     else
  780.        {    byte right_mask;
  781.         if ( bit )
  782.            {    out_g_mask(0xff >> bit);
  783.             memsetcol(rop);
  784.             rop->dest++;
  785.             w += bit - 8;
  786.            }
  787.         if ( w >= 8 )
  788.            {    out_g_mask(0xff);    /* all bits */
  789.             rop->width = w >> 3;
  790.             memsetrect(rop);
  791.             rop->dest += rop->width;
  792.             w &= 7;
  793.            }
  794.         if ( (right_mask = rmask_tab[w]) != 0 )
  795.            {    out_g_mask(right_mask);
  796.             memsetcol(rop);
  797.            }
  798.        }
  799.     set_g_const_map(0);
  800. }
  801.  
  802. /* Fill a single row specified by pointer into frame buffer, */
  803. /* starting bit within byte, and width; clean up afterwards. */
  804. #define r_m_w(ptr) (*(ptr))++        /* read & write, data irrelevant */
  805. private void near
  806. fill_row_only(byte *dest, int bit, int w, int color)
  807.   /* rop: dest */
  808. {    if ( bit + w <= 8 )
  809.        {    /* Less than one byte. */
  810.         /* Optimize filling with black or white. */
  811.         switch ( color )
  812.         {
  813.         case 0:
  814.             set_g_mask(rmask_tab[w] >> bit);
  815.             *dest &= color;        /* read, then write 0s; */
  816.                 /* some compilers optimize &= 0 to a store. */
  817.             out_g_mask(0xff);        /* dot_end */
  818.             break;
  819.         case 0xf:
  820.             set_g_mask(rmask_tab[w] >> bit);
  821.             *dest |= 0xff;        /* read, then write 1s; */
  822.                 /* some compilers optimize &= 0 to a store. */
  823.             out_g_mask(0xff);        /* dot_end */
  824.             break;
  825.         default:
  826.             set_g_const(color);
  827.             set_g_const_map(0xf);
  828.             set_g_mask(rmask_tab[w] >> bit);
  829.             r_m_w(dest);
  830.             out_g_mask(0xff);        /* dot_end */
  831.             set_g_const_map(0);
  832.         }
  833.        }
  834.     else
  835.        {    byte right_mask;
  836.         int byte_count;
  837.         set_g_const(color);
  838.         set_g_const_map(0xf);
  839.         select_g_mask();
  840.         if ( bit )
  841.            {    out_g_mask(0xff >> bit);
  842.             r_m_w(dest);
  843.             dest++;
  844.             w += bit - 8;
  845.            }
  846.         byte_count = w >> 3;
  847.         if ( (right_mask = rmask_tab[w & 7]) != 0 )
  848.            {    out_g_mask(right_mask);
  849.             r_m_w(dest + byte_count);
  850.            }
  851.         out_g_mask(0xff);
  852.         if ( byte_count )
  853.            {    memset(dest, 0, byte_count);    /* data irrelevant */
  854.            }
  855.         set_g_const_map(0);
  856.        }
  857. }
  858.